
/**********************************************************************************\
** 文件名称: flash.h
** 创建人员: YCG
** 创建日期: 2019-01-18
** 文档描述:
** 版本代号: V0.1
** 版本说明: 初始版本
\********************************************************************************/
#ifndef __FLASH_H_
#define __FLASH_H_

#include "main.h"
#include "heap.h"

//#define FLASH_DEBUG

#ifdef  FLASH_DEBUG
#include "MessageOut.h"
#define  FLASH_Printf(fmt,...)   Uprintf(fmt,__VA_ARGS__)
#else
#define  FLASH_Printf(...)
#endif

#define W25X_PageSize                   256
#define W25X_WriteEnable		            0x06
#define W25X_WriteDisable		            0x04
#define W25X_ReadStatusReg		          0x05
#define W25X_WriteStatusReg		          0x01
#define W25X_ReadData			              0x03
#define W25X_FastReadData		            0x0B
#define W25X_FastReadDual		            0x3B
#define W25X_PageProgram		            0x02
#define W25X_BlockErase			            0xD8
#define W25X_SectorErase		            0x20
#define W25X_ChipErase			            0xC7
#define W25X_PowerDown			            0xB9
#define W25X_ReleasePowerDown	          0xAB
#define W25X_DeviceID			              0xAB
#define W25X_ManufactDeviceID   	      0x90
#define W25X_JedecDeviceID		          0x9F

#define Flash_TimeOut     		          10000U
#define BUSY_Flag                       0x01  /* Write In Progress (WIP) flag */
#define DUMMY_BYTE                      0xFF
#define DUMMY_WORD                      0xFFFFFFFF
#define Flash_SUCCESS                   0x01
#define Flash_BUSY                      0x00

#define Flash_CS_Enable(yFlash)         HAL_GPIO_WritePin(yFlash->GPIO, yFlash->GPIO_Pin, GPIO_PIN_RESET)
#define Flash_CS_Disable(yFlash)        HAL_GPIO_WritePin(yFlash->GPIO, yFlash->GPIO_Pin, GPIO_PIN_SET)

typedef enum
{
    Flash_NoInit = 0x00U,   //未初始化
    Flash_Ready  = 0x01U,   //芯片准备就绪
    Flash_Tx     = 0x02U,   //正在发送
    Flash_Rx     = 0x04U,   //正在接收
    Flash_Tx_Rx  = 0x06U,   //正在发送和接收
	  Flash_Power,            //掉电模式
} Flash_State;


typedef struct FLASH_T
{
    SPI_HandleTypeDef *SPI;
    GPIO_TypeDef      *GPIO;      //按键引脚端口
    uint16_t           GPIO_Pin;  //按键引脚
    Flash_State        State;     //状态
    uint8_t            DeviceID;
    uint8_t            Type;
    uint8_t            Capacity;
    uint16_t           TimeOut;  //超时时间
    uint16_t           PageSize; //按键引脚
} Flash_t;


Flash_t * Flash_Init(SPI_HandleTypeDef *ySPI, GPIO_TypeDef *yGPIO,  uint16_t yGPIO_Pin);
uint8_t Flash_Byte(Flash_t *yFlash, uint8_t sendByte);
uint8_t Flash_WaitReady(Flash_t *yFlash);
uint8_t FLASH_SectorErase(Flash_t *yFlash, uint32_t ySectorAddr);
uint8_t FLASH_BulkErase(Flash_t *yFlash);
uint8_t FLASH_PageWrite(Flash_t *yFlash, uint8_t* yBuffer, uint32_t yWriteAddr, uint16_t ySize);
uint8_t FLASH_BufferWrite(Flash_t *yFlash, uint8_t* yBuffer, uint32_t yWriteAddr, uint16_t ySize);
uint8_t FLASH_BufferRead(Flash_t *yFlash, uint8_t* yBuffer, uint32_t yReadAddr, uint16_t ySize);
uint8_t FLASH_StartRead(Flash_t *yFlash, uint32_t yReadAddr);
uint8_t Flash_PowerDown(Flash_t *yFlash);
uint8_t Flash_WakeUp(Flash_t *yFlash);


#endif
/********************************End of File************************************/

